<template>
	<el-dialog title="客户联系对话框" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="关联客户" prop="customerId">
						<el-select
							v-model="form.customerId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入关键词"
							:remote-method="remoteMethod"
							:loading="loading"
							style="width:100%"
						>
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="回访类型" prop="linkType">
						<sc-select v-model="form.linkType" :params="{code:'linkType'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="联系人" prop="linkManId">
						<sc-select v-model="form.linkManId" :params="{customerId:form.customerId}" :apiObj="$API.crm.crmLinkMan.getCrmLinkManListForSelect" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系类型" prop="recordType">
						<el-radio-group v-model="form.recordType">
							<el-radio-button label="0">我方主动联系</el-radio-button>
							<el-radio-button label="1">对方主动联系</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="发送资料" prop="sendFile">
						<el-radio-group v-model="form.sendFile">
							<el-radio-button label="1">发送</el-radio-button>
							<el-radio-button label="0">未发</el-radio-button>
						</el-radio-group>					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="下次回访" prop="nextVisit">
						<el-date-picker v-model="form.nextVisit" placeholder="下次回访" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="联系内容" prop="remark">
				<el-input v-model="form.remark" placeholder="联系内容" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'crm'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="crmLink"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from "@/views/module/attachMenu";
export default {
	emits: ['success', 'closed'],
	components: {
		msgBoxCheckBox,
		AttachMenu
	},
	data() {
		return {
			loading:false,
			visible: false,
			isSaveing: false,
			fileList:[],
			mode: "add",
			apiObj:this.$API.file.attach.uploadFile,
			options:[],
			form: {
				customerId:'',
				linkManId:'',
				recordType:"0",
				linkType: "1",
				sendFile:"1",
				nextVisit: '',
				remark: '',
				attachId:'',
				msgType:''
			},
			rules: {
				linkManId: [
					{required: true, message: '联系人不能为空不能为空', trigger: 'blur'}
				],
				remark:[
					{required: true, message: '联系内容不能为空', trigger: 'blur'}
				],
			}
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res ;
					if (this.mode == "add") {
						res = await this.$API.crm.crmContract.insertCrmContractRecord.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.crm.crmContract.updateCrmContractRecord.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			if(data.customerId!=undefined&&data.customerId!='')
			{
				this.getMyCustomerListForSelect(data.customerId)
			}
			this.form.customerId = data.customerId;
			this.form.recordId = data.recordId
			this.form.linkManId = data.linkManId
			this.form.recordType = data.recordType
			this.form.linkType = data.linkType
			this.form.sendFile = data.sendFile
			this.form.nextVisit = data.nextVisit
			this.form.remark = data.remark
			this.form.attachId = data.attachId
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		async getMyCustomerListForSelect(customerId){
			let res = await this.$API.crm.crmCustomer.getMyCustomerListForSelect.get({customerId:customerId});
			if (res.code == 200) {
				this.options = res.data;
			}else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteMethod(keyword){
			let res = await this.$API.crm.crmCustomer.getMyCustomerListForSelect.get({keyword:keyword});
			if (res.code == 200) {
				this.options = res.data;
			}else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
